﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using cde.UserControl;
using System.Text;

public partial class main_comm_FileUpload : BasePage
{
    private const string FILE_NAME = "fileName";
    private const string IMAGE_ROWID = "rowid";
    private const string IMAGE_TABLEID = "tableid";
    private const string IMAGE_COLID = "colid";
    private const string UPLOAD_FILE = "upFile";

    private bool upFile = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString[UPLOAD_FILE] == "1")
            {
                upFile = true;
                this.hidUpfile.Value = "1";
            }

            string fileName = Request.QueryString[FILE_NAME];
            this.hidRowId.Value = Request.QueryString[IMAGE_ROWID];
            this.hidTableId.Value = Request.QueryString.Get(IMAGE_TABLEID);
            this.hidColId.Value = Request.QueryString.Get(IMAGE_COLID);

            if (!upFile)
            {
                string[] aryName;
                string path = System.Configuration.ConfigurationManager.AppSettings["IMAGE_PATH"];
                if (fileName != string.Empty && fileName != null)
                {
                    aryName = fileName.Split('|');
                    path = path + "/" + aryName[0].ToString() + "/" + aryName[1].ToString() + "_L" + "." + aryName[2].ToString();
                    this.txt_description.Value = aryName[3].ToString();
                    this.hiddPath.Value = path.ToString();
                }
            }
        }
        else
        {
            if (this.hidUpfile.Value == "1")
            {
                upFile = true;
            }
        }
        if (upFile)
        {
            this.Literal1.Text = this.lblName.Text = Resources.ResourceAdmin.ResourceManager.GetString("FILE_UPLOAD");
            this.lblLabel.Text = Resources.ResourceAdmin.ResourceManager.GetString("FILE_DESCRIPTION");
            this.preview.Visible = false;
            this.fileUpload.Attributes.Add("onchange", "checkFile()");
        }
        else
        {
            this.cmdUpload.Attributes.Add("onclick", "return checkImage()");
            this.fileUpload.Attributes.Add("onchange", "previewImage()");
        }
    }
    protected void cmdUpload_Click(object sender, EventArgs e)
    {
        //string newPath = string.Empty;
        //string filename = string.Empty;
        //string fileNameL = string.Empty;
        //string fileNameS = string.Empty;
        //string type = fileUpload.FileName.Substring(fileUpload.FileName.LastIndexOf(".") + 1);
        //string path;
        //if (upFile)
        //{
        //    path = System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["SAVE_FILE_PATH"]);
        //    if (System.IO.Directory.Exists(path))
        //    {
        //        newPath = string.Format("{0:yyyyMMdd}", DateTime.Now);
        //        filename = string.Format("{0:HHmmss}", DateTime.Now);
        //        path = path + "\\" + newPath;
        //        string fileNameC = filename + "." + type;
        //        if (System.IO.Directory.Exists(path))
        //        {
        //            this.fileUpload.SaveAs(path + "\\" + fileNameC);
        //        }
        //        else
        //        {
        //            System.IO.Directory.CreateDirectory(path);
        //            this.fileUpload.SaveAs(path + "\\" + fileNameC);
        //        }
        //    }
        //    this.hiddValue.Value = newPath + "|" + filename + "|" + type + "|" + this.txt_description.Value;
        //    if (this.hidRowId.Value != string.Empty)
        //    {
        //        CDEEntityManager em = CDEFactory.getEMByFullName("csoftechbasic.mfile");
        //        Entity entity = new Entity(em, (Oprator)Session[CDEConstants.SESSION_LOIN_OPRATOR]);
        //        entity.Operation = CDEDataOperation.INSERT;
        //        entity.set("table_id", this.hidTableId.Value);
        //        entity.set("row_id", this.hidRowId.Value);
        //        entity.set("mfile", this.hiddValue.Value);
        //        entity.set("column_id", this.hidColId.Value);

        //        List<Entity> listEntity = new List<Entity>();
        //        listEntity.Add(entity);

        //        CDEService.UpdateEntities(em, listEntity);
        //    }
        //}
        //else
        //{
        //    path = System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["SAVE_IMAGE_PATH"]);

        //    if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp")
        //    {
        //        if (System.IO.Directory.Exists(path))
        //        {
        //            newPath = string.Format("{0:yyyyMMdd}", DateTime.Now);
        //            filename = string.Format("{0:HHmmss}", DateTime.Now);
        //            path = path + "\\" + newPath;
        //            fileNameL = filename + "_L." + type;
        //            fileNameS = filename + "_S." + type;
        //            if (System.IO.Directory.Exists(path))
        //            {
        //                fileUpload.SaveAs(path + "\\" + fileNameL);
        //                SaveImageS(path, fileNameL, fileNameS);
        //            }
        //            else
        //            {
        //                System.IO.Directory.CreateDirectory(path);
        //                fileUpload.SaveAs(path + "\\" + fileNameL);
        //                SaveImageS(path, fileNameL, fileNameS);
        //            }
        //        }
        //    }
        //    else
        //    {
        //    }
        //    this.hiddValue.Value = newPath + "|" + filename + "|" + type + "|" + this.txt_description.Value;
        //    if (this.hidRowId.Value != string.Empty)
        //    {
        //        CDEEntityManager em = CDEFactory.getEMByFullName("csoftechbasic.image");
        //        Entity entity = new Entity(em, (Oprator)Session[CDEConstants.SESSION_LOIN_OPRATOR]);
        //        entity.Operation = CDEDataOperation.INSERT;
        //        entity.set("table_id", this.hidTableId.Value);
        //        entity.set("row_id", this.hidRowId.Value);
        //        entity.set("image", this.hiddValue.Value);

        //        List<Entity> listEntity = new List<Entity>();
        //        listEntity.Add(entity);

        //        CDEService.UpdateEntities(em, listEntity);
        //    }
        //}      
        //StringBuilder sbr = new StringBuilder();
        //sbr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
        //if (this.hidRowId.Value != string.Empty)
        //{
        //    sbr.AppendFormat("window.close();{0}", Environment.NewLine);
        //}
        //else
        //{
        //    sbr.AppendFormat("closeForm();{0}", Environment.NewLine);
        //}
        //sbr.AppendFormat("</script>{0}", Environment.NewLine);
        //Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sbr.ToString());
    }
    private void SaveImageS(string path, string fileNameL, string fileNameS)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(path + "\\" + fileNameL);
        System.Drawing.Image.GetThumbnailImageAbort callb = null;

        int width = image.Width;
        int height = image.Height;
        int newwidth = 40;
        int newheight = 30;

        System.Drawing.Image aNewImage = image.GetThumbnailImage(newwidth, newheight, callb, new System.IntPtr());
        aNewImage.Save(path + "\\" + fileNameS);

        image.Dispose();
        aNewImage.Dispose();
    }
}
